﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiveSequence.Interface;

namespace LiveSequence.Suite.Views
{
  public class PropertyViewManager
  {
    private static PropertyViewManager _instance;
    private object _selectedObject;
    private IDocument _document;
    public event EventHandler SelectedObjectChanged;
    public event EventHandler Refresh;

    static public PropertyViewManager Instance
    {
      get
      {
        if(_instance == null)
        {
          _instance = new PropertyViewManager();
        }
        return _instance;
      }
    }

    public object SelectedObject
    {
      get { return _selectedObject; }
      set
      {
        object oldSelectedObject = _selectedObject;
        _selectedObject = value;
        if(oldSelectedObject!=_selectedObject)
        {
          OnSelectedObjectChanged();
        }
      }
    }

    private void OnSelectedObjectChanged()
    {
      if(SelectedObjectChanged!=null)
      {
        SelectedObjectChanged(this, EventArgs.Empty);
      }
    }

    public void SetObject(IDocument document, object obj)
    {
      if((_document != document) && (_document != null))
      {
        _document.UpdateView -= _document_UpdateView;
      }

      SelectedObject = obj;

      if (document != null)
      {
        _document = document;
        _document.UpdateView += new EventHandler(_document_UpdateView);
      }
    }

    void _document_UpdateView(object sender, EventArgs e)
    {
      if(_instance.Refresh!=null)
      {
        Refresh(this, EventArgs.Empty);
      }
    }


    internal void Update()
    {
      if(_document != null)
      {
        _document.OnUpdateAllViews(null, null);
      }
    }
  }
}
